package com.btr.ivle;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.btr.ivle.R;
import com.btr.ivle.helper.DataSourceAnnouncement;

public class ViewAnnouncementActivity extends SherlockActivity{
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Sherlock_Light);
        setContentView(R.layout.post);
        
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);
        ab.setDisplayShowHomeEnabled(false);
        
        Intent intent = getIntent();
	    String ID = intent.getStringExtra(ListAnnouncementActivity.MSG_ID);
	    
	    TextView description = (TextView) findViewById(R.id.postDescription);
		TextView title = (TextView) findViewById(R.id.postTitle);
		/*
		DatabaseAnnouncement dbAnnouncement = new DatabaseAnnouncement(this,IVLEToken.getUserID()+"_Announcement",null,getResources().getInteger(R.integer.DBVersion));
        String table = IVLEToken.getUserID()+"_Announcement"; 
        String[] columns = new String[] {dbAnnouncement.Title, dbAnnouncement.Description, 
        		dbAnnouncement.CreatedDate, dbAnnouncement.CreatorName};
        String selection = dbAnnouncement.ID + "=?";
		String[] selectionArgs = new  String[] {ID};
		
        Cursor c = dbAnnouncement.getReadableDatabase().query(table, columns, 
        		selection, selectionArgs, null, null, null, null);
        c.moveToFirst();
        */
		DataSourceAnnouncement datasource = new DataSourceAnnouncement(this);
		datasource.open();
		Cursor c = datasource.getAnnouncementByID(ID);
		
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(Long.parseLong(c.getString(2)));
        Date date = calendar.getTime();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy EEE hh:mma");
        
        String text = "<p>by <strong>" + c.getString(3) + "</strong>,<br/>" + dateFormat.format(date) + "<br/></p>" + c.getString(1);
        title.setText(c.getString(0));
        description.setText(Html.fromHtml(text));
        
        c.close();
        //dbAnnouncement.close();
        datasource.close();
    }
	
	@Override
    public boolean onOptionsItemSelected(final MenuItem item) 
    {
    	switch (item.getItemId()) {
        case android.R.id.home:
          super.onBackPressed();
    	}
          return true;
    }
}
